﻿using Application.UserRoleService;
using Application.UserService;
using Domain.Entity;
using Domain.Entity.zct;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class UserController : ControllerBase
    {
        private readonly IUserService userService;
        private readonly IWebHostEnvironment webHostEnvironment;

        public UserController(IUserService userService, IWebHostEnvironment webHostEnvironment)
        {
            this.userService = userService;
            this.webHostEnvironment = webHostEnvironment;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="addUserDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<int> GetAdd(EditUserWithRolesDto editUserWithRolesDto)
        {
            return userService.GetAdd(editUserWithRolesDto);
        }

        /// <summary>
        /// 全部查
        /// </summary>
        /// <param name="fenYe"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<FenYe<User>> GetAll(int CurrentPage)
        {
            FenYe<User> fenYe = new FenYe<User>()
            {
                CurrentPage = CurrentPage,
            };
            return userService.GetAll(fenYe);
        }

        /// <summary>
        /// 条件查
        /// </summary>
        /// <param name="fenYe"></param>
        /// <param name="User_code"></param>
        /// <param name="User_name"></param>
        /// <param name="User_status"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<FenYe<User>> GetCondition(int CurrentPage, string? User_account, string? User_name, int? Position_id, int? User_status)
        {
            FenYe<User> fenYe = new FenYe<User>()
            {
                CurrentPage = CurrentPage,
            };
            return userService.GetCondition(fenYe, User_account, User_name, Position_id, User_status);
        }

        /// <summary>
        /// Id查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<User> GetID(int id)
        {
            return userService.GetID(id);
        }

        /// <summary>
        /// 停用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetDeactivated(int id)
        {
            return userService.GetDeactivated(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetDelete(int id)
        {
            return userService.GetDelete(id);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="updateUserDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEdit(EditUserWithRolesDto editUserWithRolesDto)
        {
            return userService.GetEdit(editUserWithRolesDto);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEnable(int id)
        {
            return userService.GetEnable(id);
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<UserRole> GetUser(string account, string pwd)
        {
            return await userService.GetUser(account, pwd);
        }


        /// <summary>
        /// 根据用户id查询用户
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<User> GetUserById(int uid)
        {
            return await userService.GetUserById(uid);
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="pwd">新密码</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdatePwd(int uid, string pwd)
        {
            return await userService.UpdatePwd(uid, pwd);
        }


        /// <summary>
        /// 上传头像
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost] 
        public IActionResult UploadAvatar(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("没有上传头像.");
            }

            // 生成文件名
            string name = DateTime.Now.ToString("yyyyMMddHHmmss");
            // 获取后缀名
            string ext = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            // 设置上传路径
            string path = $"Uploader/{DateTime.Now.ToString("yyyy-MM-dd-")}" + name + ext;
            // Core生成文件的绝对路径
            var uploads = Path.Combine(webHostEnvironment.WebRootPath, path);
            // 创建上传文件对应的文件夹
            (new FileInfo(uploads)).Directory.Create();
            using (var stream = new FileStream(uploads, FileMode.CreateNew))
            {
                file.CopyTo(stream); // 图片上传
                // 返回完整的可访问 URL
                string fullUrl = $"{Request.Scheme}://{Request.Host}/{path}";
                return Ok(new { success = true, Path = fullUrl });
            }
        }



        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="sex">性别</param>
        /// <param name="phone">手机号</param>
        /// <param name="avatar">头像</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateInfo(string? name, string? sex, string? phone, string? avatar, int id)
        {
            return await userService.UpdateInfo(name, sex, phone, avatar, id);
        }



        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<User>> GetAllUser()
        {
            return await userService.GetAllUser();
        }



        /// <summary>
        /// 根据部门id查询对应用户信息
        /// </summary>
        /// <param name="depid">部门编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<User>> GetUserByDeptId(int depid)
        {
            return await userService.GetUserByDeptId(depid);
        }
    }
}
